package com.maoshi.shop.mp.comment.api.util;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.maoshi.shop.mp.comment.config.bean.WxMp;
import com.maoshi.shop.mp.comment.uril.HttpGetUtil;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.HashMap;
import java.util.Map;

public class UserInfo {

    @Autowired
    private WxMp wxMp;

    public static String getToken() {
        Map<String,String> params = new HashMap<String,String>();
        params.put("appid", "wx0764ef80a5584e9a");
        params.put("secret", "16af976ef526455e29f00c7674a7a986");
        params.put("grant_type", "client_credential");

        String result = HttpGetUtil.httpRequestToString("https://api.weixin.qq.com/cgi-bin/token", params);
        JSONObject jsonObject = JSON.parseObject(result);
        //JSONObject jsonObject = JSONObject.fromObject(result);
        String access_token = jsonObject.get("access_token").toString();
        return access_token;
    }

    public static Map<String,String> getUnionId(String openid){
        Map<String,String> rel=new HashMap<String,String>();

        Map<String,String> params = new HashMap<String,String>();
        params.put("access_token",getToken());
        params.put("lang","zh_CN");
        params.put("openid",openid);
        String result = HttpGetUtil.httpRequestToString("https://api.weixin.qq.com/cgi-bin/user/info", params);
        JSONObject jsonObject = JSON.parseObject(result);
        //jsonObject = JSONObject.fromObject(result);
        System.out.println(jsonObject);
        String subscribe = jsonObject.get("subscribe").toString();
        rel.put("subscribe",subscribe);
        if(subscribe.equals("1")){
            String unionid = jsonObject.get("unionid").toString();
            String nickname = jsonObject.get("nickname").toString();
            String headimgurl = jsonObject.get("headimgurl").toString();
            String sex = jsonObject.get("sex").toString();
            rel.put("unionid",unionid);
            rel.put("nickname",nickname);
            rel.put("headimgurl",headimgurl);
            rel.put("sex",sex);
        }
        return rel;
    }


}
